package sy.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.dao.BaseDaoI;
import sy.model.Taccount;
import sy.pageModel.PageAccount;
import sy.service.TaccountServiceI;

/**
 * 
 * @author ray
 * 
 */
@Service("taccountService")
public class TaccountServiceImpl implements TaccountServiceI {

	private BaseDaoI<Taccount> baseDao;

	public BaseDaoI<Taccount> getBaseDao() {
		return baseDao;
	}

	@Autowired
	public void setBaseDao(BaseDaoI<Taccount> baseDao) {
		this.baseDao = baseDao;
	}

	public Taccount add(Taccount taccount) {
		taccount.setId(UUID.randomUUID().toString());
		this.baseDao.save(taccount);
		return taccount;
	}

	public Taccount edit(Taccount taccount) {
		Taccount taccount2 = this.baseDao.get(Taccount.class, taccount.getId());
		if (null != taccount2) {
			BeanUtils.copyProperties(taccount, taccount2);
			this.baseDao.update(taccount2);
		}
		return taccount2;
	}

	public void remove(String id) {
		for (String i : id.split(",")) {
			Taccount taccount = this.baseDao.get(Taccount.class, i);
			if (null != taccount) {
				this.baseDao.delete(taccount);
			}
		}
	}

	public List<PageAccount> treegrid() {
		List<PageAccount> pageAccounts = new ArrayList<PageAccount>();
		List<Taccount> taccounts = this.baseDao.find("from Taccount t");
		PageAccount pageAccount = null;
		if( null != taccounts && taccounts.size() > 0) {
			for (Taccount taccount : taccounts) {
				pageAccount = new PageAccount();
				BeanUtils.copyProperties(taccount, pageAccount);
				pageAccounts.add(pageAccount);
			}	
		}

		return pageAccounts;
	}

	public Taccount get(String id) {
		return this.baseDao.get(Taccount.class, id);
	}

}
